package org.hxyjs.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.Restaurant;
import org.hxyjs.entity.User;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.RestaurantService;
import org.hxyjs.utils.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 restaurantController
@RequestMapping("/restaurantController") //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /restaurantController
public class RestaurantController {
	@Resource
    RestaurantService restaurantService;
	@RequiresPermissions("restaurant:getAllRestaurant")
	@RequestMapping("/getAllRestaurant")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<Restaurant> getAllRestaurant() throws Exception{
    	System.out.println("查询全部饭店");
    	return restaurantService.getAllRestaurants();
    }

    @RequestMapping("/deleteRestaurantByRno/{rno}")
	@ResponseBody
    public DTO<Restaurant> deleteRestaurantByRno(@RequestBody @PathVariable("rno") String rno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(rno+"删除饭店");
    	return restaurantService.deleteRestaurantByRno(rno);
    }

    @RequestMapping("/addRestaurantByRno")
	@ResponseBody
	public DTO<Restaurant> addRestaurantByRno(@RequestBody Restaurant restaurant) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加饭店成功");
		return restaurantService.addRestaurantByRno(restaurant);
	}

	@RequestMapping("/updateRestaurantByRno")
	@ResponseBody
	public DTO<Restaurant> updateRestaurantByRno(@RequestBody Restaurant restaurant) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改饭店成功");
		System.out.println(restaurant);
		return restaurantService.updateRestaurantByRno(restaurant);
	}
	@RequestMapping("/getRestaurantByRno/{rno}")
	@ResponseBody
	public DTO<Restaurant> getRestaurantByRno(@PathVariable("rno") String rno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询饭店");
		return restaurantService.getRestaurantByRno(rno);
	}
	@RequestMapping("/selectRestaurantWithorderAndUser/{rno}")
	@ResponseBody
	public DTO<Restaurant> selectRestaurantWithorderAndUser(@PathVariable("rno") String rno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询成功");
		return restaurantService.selectRestaurantWithorderAndUser(rno);
	}
	@RequestMapping("/getRestaurantsByPage")
	@ResponseBody
	public DTO<Restaurant> getRestaurantsByPage(PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		String cacheId = "RestaurantByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
		return pageInfo.getCurrPage() > 2 ? restaurantService.getRestaurantsByPage(pageInfo) :
				restaurantService.getRestaurantsByPage(pageInfo,cacheId);
	}

	@RequestMapping("/getFreeRestaurantByDate/{beginDateStr}")
	@ResponseBody
	public DTO<Restaurant> getFreeRestaurantByDate(@PathVariable("beginDateStr") String beginDateStr) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询成功");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
		Date beginDate = df.parse(beginDateStr);
		Date endDate = DateUtils.addDate(beginDate, 3);
		String endDatestr = df.format(endDate);
		return restaurantService.getFreeRestaurantByDate(beginDateStr,endDatestr);
	}



}
